using HKB.Common;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
namespace HKB.Web
{
	public class Integrate
	{
		private Conn conn;
		private DataRow _config = null;
		public string Action;
		public string UserName;
		public string UserPassword;
		public string Email;
		public string Question;
		public string Answer;
		public int CookieTime;
		public string OriginPassword;
		public string Other;
		public string FrontSecurityKey;
		public string AdminSecurityKey;
		public string InterfaceType = "";
		public string PageTitle = "数据同步中";
		public string PageBody = "<div style='font-size:12px;margin:10px auto;width:98%; text-align:center;border:solid 1px #eeeeee'><img src=\"../Images/progress.gif\"/>正在读取数据</div>";
		public string ComeUrl;
		public string sendEnd;
		private string _ErrMsg = null;
		protected DataRow config
		{
			get
			{
				DataRow config;
				if (this._config == null)
				{
					this._config = new Config().config;
					config = this._config;
				}
				else
				{
					config = this._config;
				}
				return config;
			}
		}
		public string ErrMsg
		{
			get
			{
				return this._ErrMsg;
			}
		}
		public Integrate()
		{
			this.conn = new Conn();
		}
		public Integrate(Conn _conn)
		{
			this.conn = _conn;
		}
		public void Send(string _Action, string _UserName, string _UserPassword, string _Email, string _Question, string _Answer, int _CookieTime, string _OriginPassword, string _Other)
		{
			string text = "";
			string text2 = "";
			string text3 = "";
			string text4 = "";
			string text5 = DateTime.Now.ToString("yyyyMMddHH");
			string text6;
			if (this.InterfaceType.ToLower() == "admin")
			{
				if (this.AdminSecurityKey == null)
				{
					this.AdminSecurityKey = this.config["AdminSecurityKey"].ToString();
				}
				text6 = this.AdminSecurityKey;
			}
			else
			{
				if (this.FrontSecurityKey == null)
				{
					this.FrontSecurityKey = this.config["SecurityKey"].ToString();
				}
				text6 = this.FrontSecurityKey;
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<html>" + Environment.NewLine);
			stringBuilder.Append("<head>" + Environment.NewLine);
			stringBuilder.Append("<title>" + this.PageTitle + "</title>" + Environment.NewLine);
			stringBuilder.Append("</head>" + Environment.NewLine);
			stringBuilder.Append("<body onError=\"killerrors;\">" + Environment.NewLine);
			stringBuilder.Append(this.PageBody + Environment.NewLine);
			stringBuilder.Append("<script type='text/javascript'>" + Environment.NewLine);
			stringBuilder.Append("function killErrors()" + Environment.NewLine);
			stringBuilder.Append("{return true;}" + Environment.NewLine);
			stringBuilder.Append("window.onerror = killErrors;");
			stringBuilder.Append("</script>" + Environment.NewLine);
			stringBuilder.Append("<script type=\"text/javascript\">window.onError = killErrors;</script>" + Environment.NewLine);
			InterfaceBll interfaceBll = new InterfaceBll(this.conn);
			DataTable interfaceActive = interfaceBll.GetInterfaceActive(this.InterfaceType.ToLower(), _Action.ToLower());
			if (interfaceActive.Rows.Count > 0)
			{
				for (int i = 0; i < interfaceActive.Rows.Count; i++)
				{
					string text7;
					if (this.InterfaceType.ToLower() == "admin")
					{
						text7 = interfaceActive.Rows[i]["InterfaceAdminUrl"].ToString().Trim();
					}
					else
					{
						text7 = interfaceActive.Rows[i]["InterfaceFrontUrl"].ToString().Trim();
					}
					string text8 = interfaceActive.Rows[i]["CodePage"].ToString().ToLower();
					if (text8 == "" || text8 == null)
					{
						text8 = "gb2312";
					}
					string text9 = HttpUtility.UrlEncode(_UserName, Encoding.GetEncoding(text8));
					if (_Question != null)
					{
						text = HttpUtility.UrlEncode(_Question, Encoding.GetEncoding(text8));
					}
					if (_Answer != null)
					{
						text2 = HttpUtility.UrlEncode(_Answer, Encoding.GetEncoding(text8));
					}
					string text10 = FormsAuthentication.HashPasswordForStoringInConfigFile(string.Concat(new string[]
					{
						text9,
						_UserPassword.ToLower(),
						text6,
						_Action,
						text5
					}), "MD5").ToLower();
					bool flag = true;
					if (this.sendEnd != null)
					{
						if (!this.sendEnd.Contains("{#GoUrl#}"))
						{
							flag = false;
						}
					}
					if (interfaceActive.Rows[i]["CrossDomain"].ToString() == "1" && (_Action.ToLower() == "login" || _Action.ToLower() == "reg") && flag)
					{
						if (text3 == "")
						{
							if (text7 != "")
							{
								text3 = string.Concat(new object[]
								{
									text7,
									"?action=",
									_Action,
									"&SecurityKey=",
									text10,
									"&username=",
									text9,
									"&password=",
									_UserPassword,
									"&question=",
									text,
									"&answer=",
									text2,
									"&Email=",
									_Email,
									"&cookietime=",
									_CookieTime,
									"&OriginPass=",
									_OriginPassword,
									"&CrossDomain=true&other=",
									_Other,
									"&Time=",
									DateTime.Now.ToString("yyyyMMddHHmmss")
								});
							}
						}
						else
						{
							if (text7 != "")
							{
								text4 = text4 + text7 + ",";
							}
						}
					}
					else
					{
						if (_Action.ToLower() == "add" || _Action.ToLower() == "reg")
						{
							_Action = "Reg";
							stringBuilder.Append(string.Concat(new object[]
							{
								"<script >top.location='",
								text7,
								"?ComeUrl=",
								this.ComeUrl,
								"&action=",
								_Action,
								"&SecurityKey=",
								text10,
								"&username=",
								text9,
								"&password=",
								_UserPassword,
								"&question=",
								text,
								"&answer=",
								text2,
								"&email=",
								_Email,
								"&cookietime=",
								_CookieTime,
								"&OriginPass=",
								_OriginPassword,
								"&CrossDomain=false&other=",
								_Other,
								"&Time=",
								DateTime.Now.ToString("yyyyMMddHHmmss"),
								"'</script>&nbsp;",
								Environment.NewLine
							}));
						}
						else
						{
							stringBuilder.Append(string.Concat(new object[]
							{
								"<script type='text/javascript' src='",
								text7,
								"?action=",
								_Action,
								"&SecurityKey=",
								text10,
								"&username=",
								text9,
								"&password=",
								_UserPassword,
								"&question=",
								text,
								"&answer=",
								text2,
								"&email=",
								_Email,
								"&cookietime=",
								_CookieTime,
								"&OriginPass=",
								_OriginPassword,
								"&CrossDomain=false&other=",
								_Other,
								"&Time=",
								DateTime.Now.ToString("yyyyMMddHHmmss"),
								"'></script>&nbsp;",
								Environment.NewLine
							}));
						}
					}
				}
				if (string.IsNullOrEmpty(this.ComeUrl))
				{
					this.ComeUrl = this.config["SiteUrl"].ToString();
				}
				if (text3 != "")
				{
					if (text4 != "")
					{
						text4 = text4.Remove(text4.Length - 1);
					}
					text3 = string.Concat(new string[]
					{
						text3,
						"&ComeUrl=",
						this.ComeUrl,
						"&CrossUrlArray=",
						text4
					});
				}
				if (this.sendEnd != null)
				{
					if (text3 != "")
					{
						this.ComeUrl = text3;
					}
					stringBuilder.Append(this.sendEnd.Replace("{#GoUrl#}", this.ComeUrl));
				}
				else
				{
					if (text3 != "")
					{
						stringBuilder.Append("<script type='text/javascript' language='JavaScript'>top.location='" + text3 + "';</script>" + Environment.NewLine);
					}
					else
					{
						stringBuilder.Append("<script type='text/javascript' language='JavaScript'>top.location='" + this.ComeUrl + "';</script>" + Environment.NewLine);
					}
				}
				stringBuilder.Append("</body>" + Environment.NewLine);
				stringBuilder.Append("</html>");
				HttpContext.Current.Response.Write(stringBuilder.ToString());
				HttpContext.Current.Response.End();
			}
		}
		public void Send()
		{
			this.Send(this.Action, this.UserName, this.UserPassword, this.Email, this.Question, this.Answer, this.CookieTime, this.OriginPassword, this.Other);
		}
		public void SendSingle(int InterfaceID)
		{
			InterfaceBll interfaceBll = new InterfaceBll(this.conn);
			DataTable interfaceByInterfaceID = interfaceBll.GetInterfaceByInterfaceID(InterfaceID);
			if (interfaceByInterfaceID.Rows.Count <= 0)
			{
				this._ErrMsg = Language.Get("Common/NotData");
			}
			else
			{
				string text;
				string text2;
				if (this.InterfaceType.ToLower() == "admin")
				{
					if (this.AdminSecurityKey == null)
					{
						this.AdminSecurityKey = this.config["AdminSecurityKey"].ToString();
					}
					if (this.UserName == null)
					{
						this.UserName = interfaceByInterfaceID.Rows[0]["AdminLoginUrl"].ToString();
					}
					if (this.UserPassword == null)
					{
						this.UserPassword = interfaceByInterfaceID.Rows[0]["AdminLoginPassword"].ToString().ToLower();
					}
					text = interfaceByInterfaceID.Rows[0]["InterfaceAdminUrl"].ToString();
					text2 = this.AdminSecurityKey;
				}
				else
				{
					if (this.FrontSecurityKey == null)
					{
						this.FrontSecurityKey = this.config["FrontSecurityKey"].ToString();
					}
					text = interfaceByInterfaceID.Rows[0]["InterfaceAdminUrl"].ToString();
					text2 = this.FrontSecurityKey;
				}
				if (this.Action == null)
				{
					this.Action = "Login";
				}
				string text3 = DateTime.Now.ToString("yyyyMMddHH");
				string a = interfaceByInterfaceID.Rows[0]["CrossDomain"].ToString();
				string text4 = interfaceByInterfaceID.Rows[0]["CodePage"].ToString();
				if (text4 == "" || text4 == null)
				{
					text4 = "gb2312";
				}
				if (this.UserName != "" && this.UserName != null && this.UserPassword != "" && this.UserPassword != null)
				{
					string text5 = HttpUtility.UrlEncode(this.UserName, Encoding.GetEncoding(text4));
					string text6 = FormsAuthentication.HashPasswordForStoringInConfigFile(string.Concat(new string[]
					{
						text5.ToLower(),
						this.UserPassword.ToLower(),
						text2,
						this.Action,
						text3
					}), "MD5");
					string str = string.Concat(new string[]
					{
						text,
						"?action=",
						this.Action,
						"&SecurityKey=",
						text6,
						"&username=",
						text5,
						"&password=",
						this.UserPassword,
						"&Time=",
						DateTime.Now.ToString("yyyyMMddhhmmss")
					});
					if (a == "1")
					{
						HttpContext.Current.Response.Write("<script type=\"text/javascript\">window.open('" + str + "','_blank');history.back();</script>");
					}
					else
					{
						HttpContext.Current.Response.Write("<script type=\"text/javascript\">location.href='" + str + "';</script>");
					}
					HttpContext.Current.Response.End();
				}
				else
				{
					this._ErrMsg = Language.GetAdminMsg("Integrate/LoginErr");
				}
			}
		}
	}
}
